/**
 * Created by S on 2016/5/5.
 */
var commodityProxy =require('../proxy/commodityProxy');
var tools = require("../common/tool");
var async = require("async");

exports.insertcontent=function(req,res,callback){
    var param = req.body;
    var options={};
    var str="";
    async.waterfall([checkParam,checkrepeat,insertdata], function (error, result) {
        res.send(JSON.parse(tools.ResponseFormat(error, result)));
    });
    //检测参数
    function checkParam(cb) {
        if (param.name==undefined) {
            cb("name不能为空！", null);
            return;
        }
        else
        {
            options['name']=param.name;
        }
        if (param.code==undefined) {
            cb("code不能为空！", null);
            return;
        }
        else
        {
            options['code']=param.code;
        }
        if (param.costprice==undefined) {
            cb("costprice不能为空！", null);
            return;
        }
        else
        {
            options['costprice']=param.costprice;
        }
        if (param.size==undefined) {
            cb("size不能为空！", null);
            return;
        }
        else
        {
            options['size']=param.size;
        }
        if (param.unit==undefined) {
            cb("unit不能为空！", null);
            return;
        }
        else
        {
            options['unit']=param.unit;
        }
        if (param.typeid==undefined) {
            cb("typeid不能为空！", null);
            return;
        }
        else
        {
            options['typeid']=param.typeid;
        }
        if (param.textureid==undefined) {
            cb("textureid不能为空！", null);
            return;
        }
        else
        {
            options['textureid']=param.textureid;
        }
        if (param.colorid==undefined) {
            cb("colorid不能为空！", null);
            return;
        }
        else
        {
            options['colorid']=param.colorid;
        }
        if (param.sex==undefined) {
            cb("sex不能为空！", null);
            return;
        }
        else
        {
            options['sex']=param.sex;
        }
        if (param.produce==undefined) {
            cb("produce不能为空！", null);
            return;
        }
        else
        {
            options['produce']=param.produce;
        }
        if (param.addadmin==undefined) {
            cb("addadmin不能为空！", null);
            return;
        }
        else
        {
            options['addadmin']=param.addadmin;
        }
        if (param.remarks==undefined) {
            options['remarks']='';
        }
        else
        {
            options['remarks']=param.remarks;
        }
        var dd=new Date();
        options['addtime']=dd.getFullYear()+'-'+(dd.getMonth()+1) +'-'+dd.getDate();
        cb(null, null);
    }
    function checkrepeat(datas,cb)
    {
        commodityProxy.findcontent({name:options.name},function(err,data){
            if(err)
            {
                cb(err,null);
            }
            else if(!data || data.length<=0)
            {
                cb(null,null);
            }
            else
            {
                cb('商品已存在',null);
            }

        });
    }
    function insertdata(datas,cb)
    {
        commodityProxy.insertcontent(options,function(err,data){
            if(err)
            {
                cb(err,null);
            }
            else
            {
                cb(null,'添加成功');
            }
        })
    }
}

exports.findcontent = function (req, res,callback) {
    var param = req.body;
    var options={};
    async.waterfall([checkParam,search], function (error, result) {
        res.send(JSON.parse(tools.ResponseFormat(error, result)));
    });
    //检测参数
    function checkParam(cb) {
        if (param.name!=undefined) {
            options['name']=param.name;
        }
        if (param.code!=undefined) {
            options['code']=param.code;
        }
        cb(null, null);
    }
    //查找
    function search(datas,cb)
    {
        commodityProxy.findcontent(options,function (err, data) {
            if(err)
            {
                cb(err,null);
            }
            else
            {
                cb(null,data);
            }
        });
    }
};

//修改
exports.changecontent=function(req,res,callback){
    var param=req.body;
    var option={};
    async.waterfall([checkParam,checkExist,search,change],function(error,result){
        res.send(JSON.parse(tools.ResponseFormat(error,result)));
    });
    //检查参数
    function checkParam(cb)
    {
        if(tools.isInteger(param.id))
        {
            option['id']=param.id;
        }
        else
        {
            cb('id不能为空',null);
            return;
        }
        if(param.name!=undefined)
        {
            option['name']=param.name;
        }
        if(param.code!=undefined)
        {
            option['code']=param.code;
        }
        if(param.costprice!=undefined)
        {
            option['costprice']=param.costprice;
        }
        if(param.size!=undefined)
        {
            option['size']=param.size;
        }
        cb(null,null);
    }
    //是否存在
    function checkExist(datas,cb)
    {
        commodityProxy.findcontent({id:option.id},function (err, data) {
            if(err)
            {
                cb(err,null);
            }
            else if(!data || data.length<=0)
            {
                cb('项目不存在',null);
            }
            else
            {
                cb(null,null);
            }
        });
    }
    //修改信息
    function change(datas,cb)
    {
        commodityProxy.changecontent(option,function(err,data){
            if(err)
            {
                cb(err,null);
            }
            else
            {
                cb(null,'修改成功');
            }
        });
    }
}

exports.deletecontent=function(req,res,callback){
    var param=req.body;
    var option={};
    async.waterfall([checkParam,checkExist,deletes],function(error,result){
        res.send(JSON.parse(tools.ResponseFormat(error,result)));
    });
    function checkParam(cb)
    {
        if(!tools.isInteger(param.id))
        {
            cb('id不能为空',null);
        }
        else
        {
            option['id']=param.id;
            cb(null,null);
        }
    }
    //是否存在
    function checkExist(datas,cb)
    {
        commodityProxy.findcontent({id:option.id},function (err, data) {
            if(err)
            {
                cb(err,null);
            }
            else if(!data || data.length<=0)
            {
                cb('项目不存在',null);
            }
            else
            {
                cb(null,null);
            }
        });
    }
    function deletes(datas,cb)
    {
        commodityProxy.deletecontent(option,function(err,data){
            if(err)
            {
                cb(err,null);
            }
            else
            {
                cb(null,'删除成功');
            }
        });
    }
}